--region *.lua
--Date
--此文件由[BabeLua]插件自动生成
local LoginData = class("LoginData");
local Packet = require(SCRIPTS_DIR .. ".network.Packet")

function LoginData:ctor()
    self.mailgotId = 0
end

--1001
function LoginData:sendLogin(name,passwd)
    mgrs.log:printDebug("XXXXXXXXXXXX send 1001 XXXXXXXXXXXXXXXXXX")
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2L_LOGIN);
    pack:pWriteShortString(name)
    pack:pWriteShortString(passwd)
    pack:pWriteUint16(1)
    pack:pWriteUint16(enums.CurPartner)
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack());
end

--2001
function LoginData:parseLogin(data)
    mgrs.log:printDebug("############ parse 2001 ####################")
    local errorCode = data:pReadUint16()
    if errorCode == enums.Error.ERROR_OK then
        local uid = data:pReadUint32()
        local session = data:pReadShortString()
        local nickname = data:pReadShortString()
        local sex = data:pReadUint8()
        local headimgurl = data:pReadShortString()
        local gold = data:pReadUint32()
        local diamond = data:pReadUint32()
        local viplevel = data:pReadUint16()
        local vipexp = data:pReadUint16()
        mgrs.data:getPlayerData():setUid(uid)
        mgrs.data:getPlayerData():setSession(session)
        mgrs.log:printDebug("errorCode=%d uid=%s session=%s",errorCode,uid,session)
        mgrs.data:getPlayerData():setNickName(nickname)
        mgrs.data:getPlayerData():setHeadImgUrl(headimgurl)
        mgrs.data:getPlayerData():setSex(sex)
        mgrs.log:printDebug("nickname = %s headimgurl = %s sex = %d",nickname,headimgurl,sex)
        mgrs.data:getPlayerData():setVip(viplevel)
        mgrs.data:getPlayerData():setVipExp(vipexp)
        mgrs.data:getPlayerData():setGold(gold)
        mgrs.data:getPlayerData():setDiamond(diamond)
        mgrs.log:printDebug("viplevel = %d vipexp = %d gold = %d",viplevel,vipexp,gold)
        mgrs.wnd:closeWnd(enums.Wnd.Login)
        mgrs.wnd:openWnd(enums.Wnd.Loading)
        mgrs.wnd:openWnd(enums.Wnd.Hall)
    else
        mgrs.hint:showHint(errorCode)
        mgrs.hint:startShowHint();
        mgrs.proto:close(enums.Socket.Login)
    end
    
end

--1002
function LoginData:sendRegister(name,passwd)
    mgrs.log:printDebug("XXXXXXXXXXXX send 1002 XXXXXXXXXXXXXXXXXX")
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2L_REGISTER);
    pack:pWriteString(name)
    pack:pWriteString(passwd)
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack());
end

--2002
function LoginData:parseRegister(data)
    mgrs.log:printDebug("############ parse 2002 ####################")
    code = data:pReadUint16()
    mgrs.log:printDebug("parseRegister code=%d",code)
end


function LoginData:parseHallInfo(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local hallInfo = data:pReadLongString()
    dump(json.decode(hallInfo),"dumpresult",10)
end 

function LoginData:parseServerTime(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local sertime = data:pReadUint32()
    mgrs.log:printDebug("sertime=%d",sertime)
end 


--2004
function LoginData:parseGateInfo(data)
    mgrs.log:printDebug("############ parse 2004 ####################")
    local code = data:pReadUint16()
    local gateInfo = data:pReadShortString()
    gateInfo = json.decode(gateInfo)
    mgrs.proto:setIsShowHint(false)
    mgrs.proto:close(enums.Socket.Login)
    local ip = gateInfo["ip"]
    local port = gateInfo["port"]
    mgrs.log:printDebug("gateInfo = %s ip = %s port = %s",gateInfo,ip,port)
    mgrs.proto:connect(ip, port,enums.Socket.Gate);
end

--3001
function LoginData:sendLoginGate()
    mgrs.log:printDebug("XXXXXXXXXXXX send 3001 XXXXXXXXXXXXXXXXXX")
    local uid = mgrs.data:getPlayerData():getUid()
    local session = mgrs.data:getPlayerData():getSession()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2GATE_LOGIN);
    pack:pWriteUint32(uid)
    pack:pWriteShortString(session)
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Gate);
end


--4002
function LoginData:parseKickUser(data)
    mgrs.proto:close(enums.Socket.Login)
    mgrs.wnd:closeWnd(enums.Wnd.Hall)
    mgrs.wnd:openWnd(enums.Wnd.Login)
end

--6001
function LoginData:parseUserLogin(data)
    mgrs.log:printDebug("############ parse 6001 ####################")
    code = data:pReadUint16()
    mgrs.log:printDebug("parseUserLogin code = %d",code)
    mgrs.wnd:openWnd(enums.Wnd.Hall)
end

--6002
function LoginData:parseBaseInfo(data)
    mgrs.log:printDebug("############ parse 6002 ####################")
    --local uid = data:pReadUint32()
    local nickname = data:pReadShortString()
    local headimgurl = data:pReadShortString()
    local sex = data:pReadUint16()
    local gold = data:pReadUint32()
    local diamond = data:pReadUint32()
    local viplevel = data:pReadUint16()
    local vipexp = data:pReadUint16()
    mgrs.data:getPlayerData():setUid(uid)
    mgrs.data:getPlayerData():setNickName(nickname)
    mgrs.data:getPlayerData():setHeadImgUrl(headimgurl)
    mgrs.data:getPlayerData():setSex(sex)
    mgrs.data:getPlayerData():setVip(viplevel)
    mgrs.data:getPlayerData():setVipExp(vipexp)
    mgrs.data:getPlayerData():setGold(gold)
    mgrs.data:getPlayerData():setDiamond(diamond)
    mgrs.log:printDebug("nickname = %s headimgurl = %s sex = %d",nickname,headimgurl,sex)
    mgrs.log:printDebug("gold=%d diamond=%d viplevel=%d vipexp=%d",gold,diamond,viplevel,vipexp)
end

--6003
function LoginData:parseProp(data)
    mgrs.log:printDebug("############ parse 6003 ####################")
    local lc = data:pReadUint16()
    for i=1,lc do
        local id = data:pReadUint32()
        local count = data:pReadUint32()
        mgrs.log:printDebug("propid = %d count = %d",id,count)
        mgrs.data:getPlayerData():addProp(id,count)
    end
    
end

--6004
function LoginData:parseProperty(data)
    mgrs.log:printDebug("############ parse 6004 ####################")
    local lc = data:pReadUint16()
    for i=1,lc do
        local id = data:pReadUint32()
        local count = data:pReadUint16()
        mgrs.log:printDebug("propertyid = %d count = %d",id,count)
        mgrs.data:getPlayerData():setPropertyTable(id,count)
    end
    
end

--5005
function LoginData:sendCreateTable()
    mgrs.log:printDebug("XXXXXXXXXXXX send 5004 XXXXXXXXXXXXXXXXXX")
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2CT_CREATE_TABLE);
    pack:pWriteUint16(1)
    pack:pWriteUint16(4) -- 4人
    pack:pWriteUint16(8) -- 8局
    pack:pWriteUint16(1)--139
    pack:pWriteUint32(1001001)--139 
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Gate);
end

--6004
function LoginData:parseCreateTable(data)
    mgrs.log:printDebug("############ parse 6004 ####################")
    local code = data:pReadUint16()
    local tableIdStr = data:pReadShortString()
    mgrs.log:printDebug("createTable tableIdStr = %s",tableIdStr)
    self:sendEnterTable(tableIdStr)
end

--5006
function LoginData:sendEnterTable(tableStrId)
    mgrs.log:printDebug("XXXXXXXXXXXX send 5006 XXXXXXXXXXXXXXXXXX")
    mgrs.data:getPlayerData():setTableStrId(tableStrId)
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2CT_ENTER_TABLE);
    pack:pWriteShortString(tableStrId)
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Gate);
end

--6006
function LoginData:parseGameServerInfo(data)
    mgrs.log:printDebug("############ parse 6006  ####################")
    local code = data:pReadUint16()
    local ip = data:pReadShortString()
    local port = data:pReadUint16()
    local tableId = data:pReadUint16()
    local hallId = data:pReadUint16()
    mgrs.log:printDebug("code=%d,ip=%s,port=%d,tableId=%d,hallId=%d",code,ip,port,tableId,hallId)
    mgrs.data:getPlayerData():setTableId(tableId)
    mgrs.data:getPlayerData():setHallId(hallId)
    if code == 1000 then
        -- body
        mgrs.proto:connect(ip,port,enums.Socket.Game)
    end
end

function LoginData:parseCharge(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local orderid = data:pReadUint32()
    local tradeid = data:pReadUint32()
    local paytype = data:pReadUint8()
    local valueitems = data:pReadShortString()
    mgrs.log:printDebug("parseCharge %d %d %d %s",orderid,tradeid,paytype,valueitems)
end

--7001
function LoginData:LoginGame()
    mgrs.log:printDebug("XXXXXXXXXXXX send 7001 XXXXXXXXXXXXXXXXXX")
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2GAME_LOGIN);
    pack:pWriteUint16(3500);
    local uid = mgrs.data:getPlayerData():getUid()
    local session = mgrs.data:getPlayerData():getSession()
    pack:pWriteUint32(uid);
    pack:pWriteShortString(session);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Game);
end

--8001
function LoginData:parseLoginGame(data)
    mgrs.log:printDebug("############ parse 8001 ####################")
    code = data:pReadUint16()
    mgrs.log:printDebug("loginGameSucess code=%d",code)
    self:joinTable()
end

--7003
function LoginData:joinTable()
    mgrs.log:printDebug("XXXXXXXXXXXX send 7003 XXXXXXXXXXXXXXXXXX")
    local hallId = mgrs.data:getPlayerData():getHallId()
    local tableId = mgrs.data:getPlayerData():getTableId()
    local tableStrId = mgrs.data:getPlayerData():getTableStrId()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(enums.NetCmd.CMD_C2GAME_JOIN)
    pack:pWriteUint16(hallId);
    pack:pWriteUint16(tableId);
    pack:pWriteShortString(tableStrId);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Game)
end

--8002
function LoginData:parseSelfEnterGame(data)
    mgrs.log:printDebug("############ parse 8002 ####################")
    local seatnum = data:pReadUint16()
    local len = data:pReadUint16()
    mgrs.log:printDebug("myseatNum=%d other=%d",seatnum,len)
    for i = 1,len do
        local uid = data:pReadUint32()
        local seat = data:pReadUint16()
        local photo = data:pReadShortString()
        local nickname = data:pReadShortString()
        local sex = data:pReadUint8()
        local prop = data:pReadLongString()
        local property = data:pReadLongString()
        mgrs.data:getBattleData():createUser(uid,seat,photo,nickname,sex,prop,property)
        mgrs.log:printDebug("uid=%d seat=%d sex=%d",uid,seat,sex)
    end
end

--8004
function LoginData:parseOtherEnterGame(data)
    mgrs.log:printDebug("############ parse 8004 ####################")
    local uid = data:pReadUint32()
    local seat = data:pReadUint16()
    local photo = data:pReadShortString()
    local nickname = data:pReadShortString()
    local sex = data:pReadUint8()
    local prop = data:pReadLongString()
    local property = data:pReadLongString()
    mgrs.data:getBattleData():createUser(uid,seat,photo,nickname,sex,prop,property)
    mgrs.log:printDebug("uid=%d seat=%d sex=%d",uid,seat,sex)
end

--8003
function LoginData:parseJoinTable(data)
    mgrs.log:printDebug("############ parse 8003 ####################")
    local code = data:pReadUint16()
    local tableInfo = data:pReadShortString()
    mgrs.log:printDebug("parseJoinTable code=%d tableInfo=%s",code,tableInfo)
    tableInfo = json.decode(tableInfo)
    dump(tableInfo)
    mgrs.data:getPlayerData():setPlayModel(tableInfo['model'])
    mgrs.data:getPlayerData():setMaxRound(tableInfo['maxround'])
    mgrs.wnd:openWnd(enums.Wnd.Battle)
end


function LoginData:sendBindInviteCode(data)
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5001);
    pack:pWriteShortString("26945")
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parsePropertyUpdate(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local t = data:pReadUint8()
    local v1 = data:pReadUint32()
    local v2 = data:pReadUint32()
    mgrs.log:printDebug("6008 t=%d v1=%d v2=%d",t,v1,v2)
    if t == 1 then
        mgrs.log:printDebug("VIP=%d",v1)
        mgrs.data:getPlayerData():setVip(v1)
    elseif t == 2 then
        mgrs.log:printDebug("VIPEXP=%d",v1)
        mgrs.data:getPlayerData():setVipExp(v1)
    elseif t == 3 then
        mgrs.log:printDebug("Gold=%d",v1)
        mgrs.data:getPlayerData():setGold(v1)
    elseif t == 4 then
        mgrs.log:printDebug("Lottery=%d",v1)
        mgrs.data:getPlayerData():setGold(v1)
    elseif t == 5 then
        mgrs.log:printDebug("prop=%d %d",v1,v2)
        mgrs.data:getPlayerData():addProp(v1,v2)
    elseif t == 6 then
        mgrs.log:printDebug("diamond=%d",v1)
        mgrs.data:getPlayerData():setDiamond(v1)
    end
end

function LoginData:parseBindInviteCode(data,msgid)
    mgrs.log:printDebug("parseBindInviteCode msgid=%d",msgid)
    code = data:pReadUint16()
    reward = data:pReadShortString()
    mgrs.log:printDebug("6009 code=%d reward=%s",code,reward)
end

function LoginData:sendGetMail()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(1006);
    pack:pWriteUint32(self.mailgotId); 
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseGetMail(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    length = data:pReadUint16()
    mgrs.log:printDebug("parseGetMail length=%d",length)
    local mails = {}
    for i=1,length do
        local mailid = data:pReadUint32()
        local mailtype = data:pReadUint8()
        local mcindex = data:pReadUint16()
        local sendtime = data:pReadShortString()
        local valueitems = data:pReadShortString()
        local got = data:pReadUint8() 
        mgrs.log:printDebug("%d %d %d %s %s %d",mailid,mailtype,mcindex,sendtime,valueitems,got)
        if self.mailgotId < mailid then
            self.mailgotId = mailid
        end
        local mail = {mailid=mailid,mailtype=mailtype,mcindex=mcindex,sendtime=sendtime,valueitems=valueitems,got=got}
        table.insert(mails,mail)
    end
    mgrs.data:getPlayerData():addMail(mails)
    
    
    
    if length > 0 then
        local pack = Packet.new();
        pack:initWrite()
        pack:pWriteUint16(5003); 
        pack:pWriteUint32(self.mailgotId); 
        pack:pWriteEnd();
        mgrs.proto:send(pack:getPack(),enums.Socket.Gate);
    end
    
end

function LoginData:sendMailAttach()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5004); 
    pack:pWriteUint32(1); 
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Gate);
end

function LoginData:parseMailAttach(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local reward = data:pReadShortString()
    mgrs.log:printDebug("code=%d reward=%s",code,reward)
end

function LoginData:sendNewBie()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5005);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseGetNewBie(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local reward = data:pReadShortString()
    mgrs.log:printDebug("code=%d reward=%s",code,reward)
end

function LoginData:sendSignInfo()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5006);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseSignInfo(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local day = data:pReadUint16()
    local got = data:pReadUint8()
    mgrs.log:printDebug("code=%d day=%d got=%d",code,day,got)
    local hitstr = string.format("签到信息code=%d day=%d got=%d",code,day,got)
    mgrs.hint:showHint(hitstr)
    mgrs.hint:startShowHint();

    if code == 1000 and got == 0 then
        self:sendSignReward()
    end
end

function LoginData:sendSignReward()
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5007);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseSignReward(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local reward = data:pReadShortString()
    mgrs.log:printDebug("code=%d reward=%s",code,reward)

    local hitstr = string.format("获取签到奖励code=%d reward=%s",code,reward)
    mgrs.hint:showHint(hitstr)
    mgrs.hint:startShowHint();
end

function LoginData:buyItem(tradeid,paytype)
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(1005);
    pack:pWriteUint16(tradeid);
    pack:pWriteUint8(paytype);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseCreateOrder(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local orderid = data:pReadUint32()
    local paytype = data:pReadUint8()
    mgrs.log:printDebug("parseCreateOrder code=%d orderId=%d",code,orderid)

    local hitstr = string.format("下单结果code=%d orderid=%d paytype=%d",code,orderid,paytype)
    mgrs.hint:showHint(hitstr)
    mgrs.hint:startShowHint();
end

function LoginData:initHour(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local hour = data:pReadUint8()
    mgrs.log:printDebug("initHour hour=%d",hour)
end

function LoginData:sdkChargeInfo(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local tradeid = data:pReadUint16()
    local paytype = data:pReadUint16()
    local money = data:pReadUint16()
    local orderid = data:pReadUint32()
    local sign = data:pReadShortString()
    mgrs.log:printDebug("sign=%s len=%d",sign,string.len(sign))
    mgrs.pay:pay(paytype,sign)
end

function LoginData:useProp(propId,count)
    local pack = Packet.new();
    pack:initWrite()
    pack:pWriteUint16(5008);
    pack:pWriteUint32(propId);
    pack:pWriteUint8(count);
    pack:pWriteEnd();
    mgrs.proto:send(pack:getPack(),enums.Socket.Login);
end

function LoginData:parseUseProp(data,msgid)
    mgrs.log:printDebug("############### parse %d ##############",msgid)
    local code = data:pReadUint16()
    local propId = data:pReadUint32()
    local items = data:pReadShortString()
    mgrs.log:printDebug("code=%s propId=%d items=%s",code,propId,items)
end

return LoginData;

--endregion
